﻿using MES.Server.Entities;
using MES.Server.IService;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MES.Server.Service
{
    public class FuncService : BaseService, IFuncService
    {
        public FuncService(ISqlSugarClient client) : base(client)
        {
        }
        public bool CheckFuncName(string name, int id)
        {
            return _Client.Queryable<FuncEntity>()
                .Any(f => f.FuncName == name &&
                            f.FuncId != id);
        }
        public int Delete(string[] ids)
        {
            return _Client.Deleteable<FuncEntity>()
                .In(ids).ExecuteCommand();
        }

        public IEnumerable<FuncEntity> GetAllFunction(string key)
        {
            var ms = _Client.Queryable<FuncEntity>()
                .Where(m =>
                string.IsNullOrEmpty(key) || m.FuncName.Contains(key)
                )
                .ToList();

            return ms;
        }
        public IEnumerable<FuncEntity> GetFunctionByIds(string ids)
        {
            // 1,2,3,4,5
            string[] idArray = ids.Split(',');

            var ms = _Client.Queryable<FuncEntity>()
                .Where(m =>
                   idArray.Contains(m.FuncId.ToString())
                )
                .ToList();

            return ms;
        }
        public int Update(FuncEntity func)
        {
            int count;
            if (func.FuncId == 0)
            {
                // 新增
                count = _Client.Insertable(func).ExecuteCommand();
            }
            else
            {
                // 更新
                count = _Client.Updateable(func).ExecuteCommand();
            }

            return count;
        }
    }
}
